package manager;

import behavior.AgentAction;
import behavior.AgentSight;
import behavior.Behavior;
import behavior.BehaviorCalculator;
import gui.GuiData;
import gui.MainWindow;
import gui.SpaceColor;
import java.awt.Color;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import org.encog.neural.networks.BasicNetwork;
import world.*;

public class Manager 
{
    ActThread game;
    MainWindow gui;
    //Maintains reference to all the info about the game world.
    World world;
    //game uses this to get actions for agents
    BehaviorCalculator bc;
    //The current turn number. Incremented during updateBoard().
    int turn;
    
    static final int BOARD_SIZE = 50;
    
    public Manager()
    {
    }
    
    /*
    void openLoadWindow()
    Opens a window to load a saved environment
    void openConfigureWindow()
    Opens configuration window as described in (9.3)*/
    
    /**
     * Argument: 
     * -File to load from
     * Uses data from passed file to to get state of each entity and 
     * behavior of each animal
     */
//    public void loadSim(File loadMe)
//    {
//        try
//        {
//            Properties props = new Properties();
//            props.load(new FileInputStream(loadMe));
//            double vegR = Double.parseDouble(props.getProperty("vegR"));
//            double vegV = Double.parseDouble(props.getProperty("vegV"));
//            double vegC = Double.parseDouble(props.getProperty("vegC"));
//            int sizeX = Integer.parseInt(props.getProperty("sizeX"));
//            int sizeY = Integer.parseInt(props.getProperty("sizeY"));
//            
//            int numAnimals = Integer.parseInt(props.getProperty("numAnimals"));
//            Animal[] aList = new Animal[numAnimals];
//            Behavior[] bList = new Behavior[numAnimals];
//            for (int i = 0; i < aList.length; i++)
//            {
//                int locX = Integer.parseInt(props.getProperty("a" + i + "locX"));
//                int locY = Integer.parseInt(props.getProperty("a" + i + "locY"));
//                int species = Integer.parseInt(props.getProperty("a" + i + "species"));
//                int life = Integer.parseInt(props.getProperty("a" + i + "life"));
//                int hunger = Integer.parseInt(props.getProperty("a" + i + "hunger"));
//                int preg = Integer.parseInt(props.getProperty("a" + i + "preg"));
//                
//                bList[i] = new Behavior(stringToBrain(props.getProperty("a" + i + "brain")));
//                
//                float[][] offspring = stringToBrain(props.getProperty("a" + i + "child"));
//                
//                aList[i] = new Animal(locX, locY, species, 0);
//                aList[i].setLife(life);
//                aList[i].setHunger(hunger);
//                aList[i].setPregnancy(preg);
//                
//                aList[i].setOffspring(offspring);
//            }
//            
//            int numEdibles = Integer.parseInt(props.getProperty("numEdibles"));
//            Edible[] eList = new Edible[numEdibles];
//            for (int i = 0; i < eList.length; i++)
//            {
//                int locX = Integer.parseInt(props.getProperty("e" + i + "locX"));
//                int locY = Integer.parseInt(props.getProperty("e" + i + "locY"));
//                int content = Integer.parseInt(props.getProperty("e" + i + "cont"));
//                boolean type = Boolean.parseBoolean(props.getProperty("e" + i + "type"));
//                eList[i] = new Edible(locX, locY, content, type);
//            }
//        }
//        catch(IOException e)
//        {
//            unhandledException(e);
//        }
//    }
    
    public float[][][] stringToBrain(String input)
    {
        String[] layers = input.split("~");
        float[][][] data = new float[layers.length][][];
        for (int i = 0; i < layers.length; i++)
        {
            String[] thisLayer = layers[i].split(",");
            data[i] = new float[thisLayer.length][];
            for (int j = 0; j < thisLayer.length; j++)
            {
                String[] nodes = thisLayer[j].split(";");
                data[i][j] = new float[nodes.length];
                for (int k = 0; k < nodes.length; k++)
                {
                    data[i][j][k] = Float.parseFloat(nodes[k]);
                }
            }
        }
        return data;
    }
//    
//    public String brainToString( output)
//    {
//        for (int cntr = 0; cntr < network.getLayerCount()-1; cntr++)
//            {
//                for (int ndx = 0; ndx < network.getLayerNeuronCount(cntr); ndx++)
//                {
//                    for (int m = 0; m < network.getLayerNeuronCount(cntr+1); m++)
//                    {
//                        write.write(String.valueOf(network.getWeight(cntr, ndx, m)));
//                        if (m < network.getLayerNeuronCount(cntr+1)-1 || ndx < network.getLayerNeuronCount(cntr)-1 || cntr < network.getLayerCount()-2)
//                        {
//                            write.write(",");
//                        }
//                    }
//                }
////                if (cntr < network.getLayerCount()-2)
////                {
////                    write.write(network.getLayerBiasActivation(cntr) + ", ");
////                }
//            }
//            write.flush();
//        return out;
//    }
    
    /**
     * Argument:
     * -File to save to
     * Saves state of each entity to file and each animal’s behavior
     */
    public void saveSim(File saveToMe)
    {
        try
        {
            Properties props = new Properties();
            props.setProperty("simType", "local");
            props.setProperty("vRate", String.valueOf(world.getVeggieRate()));
            props.setProperty("vVar", String.valueOf(world.getVeggieVar()));
            props.setProperty("vCont", String.valueOf(world.getVeggieContent()));
            props.setProperty("xSize", String.valueOf(world.getxSize()));
            props.setProperty("ySize", String.valueOf(world.getySize()));
            props.setProperty("numAni", String.valueOf(world.getNumAnimals()));
            for (int i = 0; i < world.getNumAnimals(); i++)
            {
                Animal a = world.getAnimalByIndex(i);
                props.setProperty("ani" + i + "x", String.valueOf(a.getX()));
                props.setProperty("ani" + i + "y", String.valueOf(a.getY()));
                props.setProperty("ani" + i + "spec", String.valueOf(a.getSpecies()));
                props.setProperty("ani" + i + "life", String.valueOf(a.getLife()));
                props.setProperty("ani" + i + "hunger", String.valueOf(a.getHunger()));
                props.setProperty("ani" + i + "preg", String.valueOf(a.getPregnancy()));
                props.setProperty("ani" + i + "life", String.valueOf(a.getLife()));
                props.setProperty("beha" + i + "brain", bc.getBrainString(i));
                props.setProperty("ani" + i + "child", a.getOffspring().toString());
                props.setProperty("ani" + i + "gen", String.valueOf(a.getGeneration()));
                props.setProperty("ani" + i + "born", String.valueOf(a.getBirthday()));
                
            }
            
            props.setProperty("numEdi", String.valueOf(world.getNumEdibles()));
            for (int i = 0; i < world.getNumEdibles(); i++)
            {
                Edible e = world.getEdibleByIndex(i);
                props.setProperty("eat" + i + "fd", String.valueOf(e.getFood()));
                props.setProperty("eat" + i + "x", String.valueOf(e.getX()));
                props.setProperty("eat" + i + "x", String.valueOf(e.getY()));
                props.setProperty("eat" + i + "diet", String.valueOf(e.getType()));
            }
            
            props.setProperty(("numSpecies"), String.valueOf(world.getNumSpecies()));
            for (int i = 0; i < world.getNumSpecies(); i++)
            {
                Species s = world.getSpeciesByIndex(i);
                props.setProperty("sp" + i + "atk", String.valueOf(s.getAttackPower()));
                props.setProperty("sp" + i + "carc", String.valueOf(s.getCarcassFood()));
                props.setProperty("sp" + i + "red", String.valueOf(s.getColor().getRed()));
                props.setProperty("sp" + i + "blu", String.valueOf(s.getColor().getBlue()));
                props.setProperty("sp" + i + "grn", String.valueOf(s.getColor().getGreen()));
                props.setProperty("sp" + i + "eat", String.valueOf(s.isDiet()));
                props.setProperty("sp" + i + "fh", String.valueOf(s.getFightHunger()));
                props.setProperty("sp" + i + "preg", String.valueOf(s.getGestation()));
                props.setProperty("sp" + i + "mah", String.valueOf(s.getMateHunger()));
                props.setProperty("sp" + i + "hunger", String.valueOf(s.getMaxHunger()));
                props.setProperty("sp" + i + "life", String.valueOf(s.getMaxLife()));
                props.setProperty("sp" + i + "moh", String.valueOf(s.getMoveHunger()));
                props.setProperty("sp" + i + "mov", String.valueOf(s.getMovementRate()));
                props.setProperty("sp" + i + "name", String.valueOf(s.getName()));
                props.setProperty("sp" + i + "heal", String.valueOf(s.getRegenRate()));
                props.setProperty("sp" + i + "view", String.valueOf(s.getSightRad()));
                props.setProperty("sp" + i + "wh", String.valueOf(s.getWaitHunger()));
            }
            
            props.store(new FileOutputStream(saveToMe), "");
        }
        catch(IOException e)
        {
            unhandledException(e, "An error occured saving to file.");
        }
    }
    
    /**
     * Argument:
     * -List of species in the game
     * -x and y sizes of map
     * -veggie spawn rate
     * -veggie content
     * Initializes world with passed values and calls 
     * initBehavior in bc, then begins the game.
     */
    public void generateSim(String file)
    {
        Properties props = new Properties();
        try
        {
            FileInputStream fis = new FileInputStream(file);
            props.load(fis);
            fis.close();
        }
        catch(IOException e)
        {
            unhandledException(e, "An error occured loading from the file.");
        }
        Species[] list = new Species[Integer.parseInt(props.getProperty("numSpecies"))];
        
//        while (props.propertyNames().())
//        {
//            System.out.println(props.propertyNames().);
//        }
        for (int i = 0; i < list.length; i++)
        {
            Species s = list[i];
            s = new Species();
            s.setAttackPower(Integer.parseInt(props.getProperty("sp" + i + "atk")));
            s.setCarcassFood(Integer.parseInt(props.getProperty("sp" + i + "carc")));
            s.setColor(new Color(Integer.parseInt(props.getProperty("sp" + i + "red")), 
                    Integer.parseInt(props.getProperty("sp" + i + "blu")), 
                    Integer.parseInt(props.getProperty("sp" + i + "grn"))));
            s.setDiet(Boolean.parseBoolean(props.getProperty("sp" + i + "eat")));
            s.setFightHunger(Integer.parseInt(props.getProperty("sp" + i + "fh")));
            s.setGestation(Integer.parseInt(props.getProperty("sp" + i + "preg")));
            s.setIndex(i);
            s.setMateHunger(Integer.parseInt(props.getProperty("sp" + i + "mah")));
            s.setMaxHunger(Integer.parseInt(props.getProperty("sp" + i + "hunger")));
            s.setMaxLife(Integer.parseInt(props.getProperty("sp" + i + "life")));
            s.setMoveHunger(Integer.parseInt(props.getProperty("sp" + i + "moh")));
            s.setMovementRate(Integer.parseInt(props.getProperty("sp" + i + "mov")));
            s.setName(props.getProperty("sp" + i + "name"));
            s.setRegenRate(Integer.parseInt(props.getProperty("sp" + i + "heal")));
            s.setSightRad(Integer.parseInt(props.getProperty("sp" + i + "view")));
            s.setWaitHunger(Integer.parseInt(props.getProperty("sp" + i + "wh")));
            s.setMercyAmt(Integer.parseInt(props.getProperty("sp" + i + "mercy")));
            list[i] = s;
        }
        
        int xSize = Integer.parseInt(props.getProperty("xSize"));
        int ySize = Integer.parseInt(props.getProperty("ySize"));
        double veggieRate = Double.parseDouble(props.getProperty("vRate"));
        int veggieCont = Integer.parseInt(props.getProperty("vCont"));
        int curTurn = Integer.parseInt(props.getProperty("turn"));
        double veggieVar = Double.parseDouble(props.getProperty("vVar"));

        world = new World(list, xSize, ySize, veggieRate, veggieCont, curTurn, veggieVar, this, file);
        bc = BehaviorCalculator.factory(this, Double.parseDouble(props.getProperty("mutFac")), file);
//        bc.setMutationFactor(mutFac);
//        bc.initBehavior();
        game = new ActThread(this);
        gui = new MainWindow(this, -(xSize/2), -(ySize/2));
        game.start();
        gui.setVisible(true);
    }
    
    /**
     * Calls procActions in bc
     **/
    public void procActions()
    {
        bc.procActions();
    }
    
    public int[] getSelfProcData(int ndx)
    {
        int[] r = new int[3];
        Animal a = world.getAnimalByIndex(ndx);
        r[0] = a.getLife();
        r[1] = a.getHunger();
        r[2] = a.getPregnancy();
        return r;
    }
    
    /**
     * Argument: index of animal seeing.
     * Returns: Array of spaces the agent sees.
     * Calls getViewedSpaces in World passing index. Loops through 
     * resulting array calling getOccupant in World for each x and 
     * y value. Translates result into an AgentSight object and adds it 
     * to an AgentSight array. Returns this array.
     */
    public AgentSight[] getAgentView(int index)
    {
        int[][] spaces = world.getAnimalView(index);
        AgentSight[] ofTheJedi = new AgentSight[spaces.length];
        for (int i = 0; i <  spaces.length; i++)
        {
            Entity b = world.getOccupant(spaces[i][0], spaces[i][1]);
            if (b != null)
            {
                ofTheJedi[i] = b.getAgentSight();
            }
            else
            {
                ofTheJedi[i] = new AgentSight();
                ofTheJedi[i].setSpecies(-3);
                ofTheJedi[i].setLife(0);
                ofTheJedi[i].setPregnant(false);
            }
        }
        
        return ofTheJedi;
    }
    
    /**
     * Argument:
     * -Index of animal to set action for
     * -Action for animal to perform, as well as defined target.
     * Calls getAnimal in World and gets the x and y coordinates for 
     * it. Adds that to the coordinates in the AgentAction parameter. 
     * Then calls updateAction with the AgentAction param’s action 
     * property and passed index. Then calls updateTarget with 
     * AgentAction param’s x and y properties and passed index.
     */
    public void setAction(int index, AgentAction act)
    {
        Animal a = world.getAnimalByIndex(index);
        act.setX(act.getX() + a.getX());
        act.setY(act.getY() + a.getY());
        world.updateAction(index, act.getAction());
        world.updateTarget(index, act.getX(), act.getY());
    }
    
    /**
     * Calls spawnVeggies() on world.
     */
    public void spawnVeggies()
    {
        world.spawnVeggies();
    }
    
    /**
     * Calls finalizeTurn() on world, then increments turn.
     */
    public void updateBoard()
    {
        world.finalizeTurn();
        System.out.print(turn+ ": ");
        turn++;
    }
    
    /**
     * Argument: weights of behavior to be added.
     * Calls addBehavior in bc passing argument.
     */
    public void addBehavior(Behavior net)
    {
        bc.addBehavior(net);
    }
    
    /**
     * Argument:
     * -distance from center of circle, 1 being adjacent squares
     * -spot on perimeter
     * Returns:
     * two element array containing the x and y values of that spot 
     * relative to the center.
     */
    public int[] getFromCircle(int dist, int spot)
    {
        int perim = 8 * dist;
        spot = spot % perim;
        final int INC_X = 0;
        final int DEC_Y = 1;
        final int DEC_X = 2;
        final int INC_Y = 3;
        
        int action = INC_X;
        int[] returnMe = {0, dist};
        
        for (int cntr = 0; cntr < spot; cntr++)
        {
            if (action == INC_X)
            {
                returnMe[0]++;
                if (returnMe[0]>=dist)
                {
                    action++;
                }
            }
            else if (action == DEC_Y)
            {
                returnMe[1]--;
                if (returnMe[1]<=dist)
                {
                    action++;
                }
            }
            else if (action == DEC_X)
            {
                returnMe[0]--;
                if (returnMe[0]<=dist)
                {
                    action++;
                }
            }
            else if (action == INC_Y)
            {
                returnMe[1]--;
                if (returnMe[1]<=dist)
                {
                    action++;
                }
            }
        }
        return returnMe;
    }
    
    /**
     * Argument:
     * -minimum x value
     * -minimum y value
     * Returns:
     * Color and location of every entity with location values greater than or 
     * equal to the params and less than the params - 51.
     */
    public GuiData getGuiData(int minX, int minY)
    {
        ArrayList<SpaceColor> blocks = new ArrayList<SpaceColor>();
        for (int i = 0; i < world.getNumAnimals(); i++)
        {
            int aniX = world.getAnimalByIndex(i).getX();
            int aniY = world.getAnimalByIndex(i).getY();
            if (aniX >= minX && aniX <= minX + BOARD_SIZE && aniY >= minY && aniY <= minY+BOARD_SIZE)
            {
                Color c = world.getSpeciesByIndex(world.getAnimalByIndex(i).getSpecies()).getColor();
                blocks.add(new SpaceColor(c, aniX - minX, aniY - minY));
            }
        }
        
        for (int i = 0; i < world.getNumEdibles(); i++)
        {
            int ediX = world.getEdibleByIndex(i).getX();
            int ediY = world.getEdibleByIndex(i).getY();
            if (ediX >= minX && ediX <= minX + BOARD_SIZE && ediY >= minY && ediY <= minY+BOARD_SIZE)
            {
                if (world.getEdibleByIndex(i).getType() == Edible.VEGGIE)
                {
                    blocks.add(new SpaceColor(Color.GREEN, ediX - minX, ediY - minY));
                }
                else
                {
                    blocks.add(new SpaceColor(Color.PINK, ediX - minX, ediY - minY));
                }
            }
        }
        int numVeg = world.getNumEdibleByType(Edible.VEGGIE);
        int numCarc = world.getNumEdibleByType(Edible.MEAT);
        GuiData ofTheJedi = new GuiData(blocks.toArray(new SpaceColor[blocks.size()]), bc.getMutationFactor(),
                numVeg, numCarc, world.getVeggieRate(), world.getxSize(), world.getySize(), world.animalCountBySpecies());
       
        return ofTheJedi;
    }
    
    /**
     * Argument:
     * -The exception thrown
     * Calls “show exception” in MainWindow with the 
     * text of the exception thrown.
     */
    public void unhandledException(Exception e, String msg)
    {
        String error = new String();
        StackTraceElement[] ste = e.getStackTrace();
        for (int i = 0; i < ste.length; i++)
        {
            error = error + ste[i].toString() + "\n";
        }
        gui.showException(error, msg);
    }
    /**
     * Argument:
     * -The x and  y coords of the entity
     * Returns
     * -Entity at passed coordinates
     * Returns world.getOccupantByLocation
     */
    public Entity getOccupantByLocation(int x, int y)
    {
        return world.getOccupant(x, y);
    }
    
    /**
     * If game is currently running, calls haltGame in ActThread. Otherwise, calls unHaltGame in ActThread.
     */
    public void pauseUnpause()
    {
        if (game.continueRunning)
        {
            game.haltGame();
        }
        else
        {
            game.unHaltGame();
        }
    }
    
    /**
     * Argument
     * -Index of desired species
     * Returns
     * -Reference to actual species
     * Calls getSpeciesByIndex in World
     */
    public Species getSpecies(int i)
    {
        return world.getSpeciesByIndex(i);
    }

    /**
     * Calls saveSim, then calls stop in game and closes gui
     */
    public void exit()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddhhmm");
        saveSim(new File(sdf.format(new Date())));
        game.haltGame();
        gui.setVisible(false);
        System.exit(0);
    }
    
    /**
     * Argument:
     * -String array of command line arguments
     * Loops through argument. If an element reads “-gen” calls 
     * generateSim passing next element. If an element reads 
     * “-load” calls loadSim passing the next element
     */
    void parseCLA(String[] cla)
    {
        for (int i = 0; i < cla.length; i++)
        {
            if (cla[i].equals("-load"))
            {
                generateSim(cla[i+1]);
            }
        }
    }
    
    /**
     * Arguments:
     * -index of animal killed
     * Calls removeBehavior in bc
     */
    public void kill(int index)
    {
        bc.removeBehavior(index);
    }
    
    public BasicNetwork generateBehavior(int f, int m)
    {
        Animal dad = world.getAnimalByIndex(f);
        Animal mom = world.getAnimalByIndex(m);
        return bc.generateBehavior(f, m, mom.getX(), mom.getY(), world.getxSize(), world.getySize(),
                mom.getGeneration(), dad.getGeneration(), world.getSpeciesByIndex(dad.getSpecies()).getMaxGen(), 
                turn-mom.getBirthday(), 100);
    }
    
    public static void doIt()
    {
        try
        {
            Properties props = new Properties();
            props.setProperty("numAni", "15");
            BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream("brain1.txt")));
            for (int i = 0; i < 10; i++)
            {
                props.setProperty("ani" + i + "life", "100");
                props.setProperty("ani" + i + "hung", "100");
                props.setProperty("ani" + i + "x", String.valueOf(i/2));
                props.setProperty("ani" + i + "y", String.valueOf(i%2));
                props.setProperty("ani" + i + "spec", "1");
                props.setProperty("ani" + i + "preg", "-1");
                props.setProperty("ani" + i + "child", "");
                props.setProperty("ani" + i + "gen", "0");
                props.setProperty("ani" + i + "born", "0");
                props.setProperty("ani" + i + "life", "100");
                props.setProperty("ani" + i + "life", String.valueOf(i));
                props.setProperty("beha" + i + "brain", read.readLine());
            }
            read = new BufferedReader(new InputStreamReader(new FileInputStream("brain.txt")));
            for (int i = 10; i < 15; i++)
            {
                props.setProperty("ani" + i + "life", "100");
                props.setProperty("ani" + i + "hung", "100");
                props.setProperty("ani" + i + "x", String.valueOf(-i/2));
                props.setProperty("ani" + i + "y", String.valueOf(-i%2));
                props.setProperty("ani" + i + "spec", "0");
                props.setProperty("ani" + i + "preg", "-1");
                props.setProperty("ani" + i + "child", "");
                props.setProperty("ani" + i + "gen", "0");
                props.setProperty("ani" + i + "born", "0");
                props.setProperty("ani" + i + "life", "100");
                props.setProperty("ani" + i + "life", String.valueOf(i));
                props.setProperty("beha" + i + "brain", read.readLine());
            }
            props.setProperty("numSpecies", "2");
            props.setProperty("simtype", "local");
            props.setProperty("sp0atk", "25");
            props.setProperty("sp0carc", "100");
            props.setProperty("sp0red", "176");
            props.setProperty("sp0grn", "23");
            props.setProperty("sp0blu", "31");
            props.setProperty("sp0eat", "true");
            props.setProperty("sp0fh", "3");
            props.setProperty("sp0preg", "15");
            props.setProperty("sp0mah", "8");
            props.setProperty("sp0hunger", "100");
            props.setProperty("sp0life", "100");
            props.setProperty("sp0moh", "2");
            props.setProperty("sp0mov", "5");
            props.setProperty("sp0name", "Pred");
            props.setProperty("sp0heal", "15");
            props.setProperty("sp0view", "6");
            props.setProperty("sp0wh", "1");
            props.setProperty("sp0mercy", "6");
           
            
            props.setProperty("sp1atk", "5");
            props.setProperty("sp1carc", "100");
            props.setProperty("sp1red", "0");
            props.setProperty("sp1grn", "245");
            props.setProperty("sp1blu", "255");
            props.setProperty("sp1eat", "true");
            props.setProperty("sp1fh", "5");
            props.setProperty("sp1preg", "10");
            props.setProperty("sp1mah", "8");
            props.setProperty("sp1hunger", "100");
            props.setProperty("sp1life", "100");
            props.setProperty("sp1moh", "2");
            props.setProperty("sp1mov", "3");
            props.setProperty("sp1name", "Prey");
            props.setProperty("sp1heal", "8");
            props.setProperty("sp1view", "4");
            props.setProperty("sp1wh", "1");
            props.setProperty("sp1mercy", "3");
            
            props.setProperty("xSize", "20");
            props.setProperty("ySize", "20");
            props.setProperty("vRate", ".1");
            props.setProperty("vCont", "100");
            props.setProperty("turn", "0");
            props.setProperty("vVar", "0");
            props.setProperty("simtype", "local");
            props.setProperty("mutFac", ".2");
            
            props.setProperty("numEdi", "2");
            
            props.setProperty("eat0x", "2");
            props.setProperty("eat0y", "2");
            props.setProperty("eat0fd", "100");
            props.setProperty("eat0diet", "false");
            
            props.setProperty("eat1x", "6");
            props.setProperty("eat1y", "2");
            props.setProperty("eat1fd", "100");
            props.setProperty("eat1diet", "false");
            props.store(new FileOutputStream("test"), "");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] psvm)
    {
//        doIt();
        
        psvm = new String[2];
        psvm[0] = "-load";
        psvm[1] = "test";
        Manager mgr = new Manager();
        try
        {
            mgr.parseCLA(psvm);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            //mgr.unhandledException(e, "An unknown error has occured");
        }               
    }
}
